var _NORMAL = "0000000";
var layuiGrid1DataCache = [];
var layuiGrid2DataCache = [];
var iary1Cache = [];
var iary2Cache = [];

/**
 * 获取表格1数据
 */
function getGrid1Data(iary) {
    if (iary === undefined) {
        iary = {
            data_cate : 'FDBB'
        };
    }
    var inTrxObj = {
        trx_id          : 'XPBASICDATA',
        action_flg      : 'Q',
        iary            : [iary]
    };
    var outTrxObj = comTrxSubSendPostJson(inTrxObj);
    if(outTrxObj.rtn_code == _NORMAL) {
        layuiGrid1DataCache = outTrxObj.oary;
        _layuiGrid.f.setGridData('layuiGrid1', outTrxObj.oary);
    }
}

/**
 * 获取表格2数据
 */
function getGrid2Data(iary) {
    if (iary === undefined) {
        iary = {
            data_cate : 'EFGR'
        };
    }
    var inTrxObj = {
        trx_id          : 'XPBASICDATA',
        action_flg      : 'Q',
        iary            : [iary]
    };
    var outTrxObj = comTrxSubSendPostJson(inTrxObj);
    if(outTrxObj.rtn_code == _NORMAL) {
        layuiGrid2DataCache = outTrxObj.oary;
        _layuiGrid.f.setGridData('layuiGrid2', outTrxObj.oary);
    }
}

/**
 * 新增分档版本数据
 */
function addGridData(iary) {
    if (iary === undefined) {
        iary = {}
    }
    var inTrxObj = {
        trx_id          : 'XPBASICDATA',
        action_flg      : 'A',
        iary            : [iary]
    };
    var outTrxObj = comTrxSubSendPostJson(inTrxObj);
    if(outTrxObj.rtn_code == _NORMAL) {
        layer.msg('新增分档版本数据成功');
        layuiGrid2DataCache = [];
        _layuiGrid.f.cleanGridData('layuiGrid2');
        getGrid1Data();
    }
}

/**
 * 删除表格数据
 */
function deleteGridData(dataArrays) {
    if(0 === dataArrays.length) {
        layer.msg('没有需要删除的数据', function(){});
        return;
    }
    var iaryI = [];
    for(var i=0; i<dataArrays.length; i++){
        var rowData = dataArrays[i];
        var iary = {
            data_seq_id : rowData.dataSeqId,
            evt_user    : $("#userSpan").text()
        };
        iaryI.push(iary);
    }
    var inTrxObj = {
        trx_id     : 'XPBASICDATA',
        action_flg : 'D',
        iary       : iaryI
    };
    var outTrxObj = comTrxSubSendPostJson(inTrxObj);
    if(outTrxObj.rtn_code == _NORMAL) {
        layer.msg('数据删除成功');
    }
}

/**
 * 修改表格数据
 */
function updateGridData(dataArrays) {
    if(0 === dataArrays.length) {
        layer.msg('没有需要修改的数据', function(){});
        return;
    }
    var iaryI = [];
    for(var i=0; i<dataArrays.length; i++){
        var iary = dataArrays[i];
        iary['evt_user'] = $("#userSpan").text();
        iaryI.push(iary);
    }
    var inTrxObj = {
        trx_id     : 'XPBASICDATA',
        action_flg : 'U',
        iary       : iaryI
    };
    var outTrxObj = comTrxSubSendPostJson(inTrxObj);
    if(outTrxObj.rtn_code == _NORMAL) {
        layer.msg('数据修改成功');
        getGridData();
    }
};

/**
 * 获取参数列设定
 */
function getTableCols() {
    var dataCate = $(".leftNav .layui-this a").text().substring(0, 4);
    if ("EFGR" === dataCate) {
        return [[
            {width:50, title: '复选框', type: 'checkbox'}
            ,{field:'operation', width:140, title: '操作',      toolbar:"#tableBar"}
            ,{field:'dataSeqId', width:150, title: 'dataSeqId', sort: true, hide: true}
            ,{field:'dataCate',  width:150, title: 'dataCate',  sort: true, hide: true}
            ,{field:'dataExt',   width:120, title: '分档版本',  sort: true, edit: true}
            ,{field:'dataId',    width:120, title: '单/多晶',   sort: true, edit: true}
            ,{field:'ext1',      width:120, title: '效率' ,     sort: true, edit: true}
            ,{field:'ext2',      width:120, title: 'Bin值',     sort: true, edit: true}
            // ,{field:'ext3',      minWidth:100, title: '对应线别',  sort: true, edit: true}
            ,{field:'evtUsr',       width:120, title: '修改人',   sort: true, edit: true}
            ,{field:'evtTimestamp', width:160, title: '修改时间', sort: true, edit: true}
        ]];
    } else if ("ETTA" === dataCate) {
        return [[
            {width:50, title: '复选框', type: 'checkbox'}
            ,{field:'operation', width:140, title: '操作',      toolbar:"#tableBar"}
            ,{field:'dataSeqId', width:150, title: 'dataSeqId', sort: true, hide: true}
            ,{field:'dataCate',  width:150, title: 'dataCate',  sort: true, hide: true}
            ,{field:'dataId',    width:150, title: '年',        sort: true, edit: true}
            ,{field:'ext1',      width:150, title: '月' ,       sort: true, edit: true}
            ,{field:'ext2',      width:150, title: '目标效率值',sort: true, edit: true}
            ,{field:'ext3',      width:150, title: '单/多晶',   sort: true, edit: true}
        ]];
    } else if ("LIAT" === dataCate) {
        return [[
            {width:50, title: '复选框', type: 'checkbox'}
            ,{field:'operation', width:140, title: '操作',      toolbar:"#tableBar"}
            ,{field:'dataSeqId', width:150, title: 'dataSeqId', sort: true, hide: true}
            ,{field:'dataCate',  width:150, title: 'dataCate',  sort: true, hide: true}
            ,{field:'dataId',    width:150, title: '线别',      sort: true, edit: true}
            ,{field:'ext1',      width:150, title: '单/多晶' ,  sort: true, edit: true}
        ]];
    } else if ("MOMA" === dataCate) {
        return [[
            {width:50, title: '复选框', type: 'checkbox'}
            ,{field:'operation', width:140, title: '操作',      toolbar:"#tableBar"}
            ,{field:'dataSeqId', width:150, title: 'dataSeqId', sort: true, hide: true}
            ,{field:'dataCate',  width:150, title: 'dataCate',  sort: true, hide: true}
            ,{field:'dataId',    width:150, title: '型号/规格', sort: true, edit: true}
            ,{field:'ext1',      width:150, title: '物料号' ,   sort: true, edit: true}
        ]];
    } else if ("WIPV" === dataCate) {
        return [[
            {width:50, title: '复选框', type: 'checkbox'}
            ,{field:'operation', width:140, title: '操作',      toolbar:"#tableBar"}
            ,{field:'dataSeqId', width:150, title: 'dataSeqId', sort: true, hide: true}
            ,{field:'dataCate',  width:150, title: 'dataCate',  sort: true, hide: true}
            ,{field:'dataExt',   width:150, title: '统计时间',  sort: true, edit: true}
            ,{field:'dataId',    width:150, title: '型号/规格', sort: true, edit: true}
            ,{field:'ext1',      width:150, title: '工段' ,     sort: true, edit: true}
            ,{field:'ext2',      width:150, title: 'WIP值' ,    sort: true, edit: true}
        ]];
    } else {
        return [];
    }
}

/**
 * 初始化事件
 */
function initEvents() {
    //按钮功能提示框
    $("#query_btn").on('mouseover', function(){
        layer.tips('按条件查询数据 ', this, {
            tips: 1
        });
    });

    //查询分档版本
    $("#queryFDBB_btn").on('click', function(){
        layuiGrid2DataCache = [];
        _layuiGrid.f.cleanGridData('layuiGrid2');
        getGrid1Data();
    });

    //新增型号/规格-物料号模态框弹出
    $("#addFDBB_btn").on('click', function(){
        $("#addFDBBModal input").val("");
        $("#addFDBBModalLabel").text("新增分的版本数据");
        $("#addFDBBModal").modal("show");
    });

    //新增型号/规格-物料号
    $("#addFDBBModal_btn").on('click', function(){
        var iary = {
            dataCate        : 'FDBB',
            dataId	        : $("#fdbb_input").val(),
            ext1            : $("#dyxb_input").val(),
            evt_usr         : $("#userSpan").text()
        };
        addGridData(iary);
        $("#addFDBBModal").modal("hide");
    });

    //删除分档版本
    $("#delFDBB_btn").on('click', function () {
        layer.open({
            content: '是否确认删除?'
            , btn: ['删除选中行', '删除表格中数据', '取消']
            , btn1: function (index, layero) {
                var checkStatus = layui.table.checkStatus('layuiGrid1');
                deleteGridData(checkStatus.data);
                getGrid1Data();
            }
            , btn2: function (index, layero) {
                deleteGridData(layuiGrid1DataCache);
                getGrid1Data();
            }
            , btn3: function (index, layero) {

            }
        });
    });

    //删除效率
    $("#delEFGR_btn").on('click', function () {
        layer.open({
            content: '是否确认删除?'
            , btn: ['删除选中行', '删除全部行', '取消']
            , btn1: function (index, layero) {
                var checkStatus = layui.table.checkStatus('layuiGrid2');
                deleteGridData(checkStatus.data);
                getGrid2Data(iary2Cache);
            }
            , btn2: function (index, layero) {
                deleteGridData(layuiGrid2DataCache);
                getGrid2Data(iary2Cache);
            }
            , btn3: function (index, layero) {

            }
        });
    });

    //Excel导入模态框弹出
    $("#uploadEFGRModal_btn").on('click', function(){
        $("#fileinput").val("");
        $("#versionId").val("");
        $("#uploadModal").modal("show");
        $("#upload_btn").attr("disabled",false);
    });

    //Excel上传按钮
    $("#upload_btn").on('click', function(){
        var fileDir = $("#fileinput").val();
        var suffix = fileDir.substr(fileDir.lastIndexOf("."));
        if("" == fileDir){
            layer.msg('选择需要导入的Excel文件!', function(){});
            return false;
        }
        if(".xls" != suffix && ".xlsx" != suffix ){
            layer.msg('选择Excel格式的文件导入!', function(){});
            $('#fileinput').val("");
            return false;
        }

        var dataCate = 'EFGR';

        if (dataCate !== null && dataCate !== '' && dataCate !== undefined) {
            $("#upload_btn").attr("disabled",true);
            $("#uploadForm").ajaxSubmit({
                url  : '/jn-report/uploadBisData.do',
                type : 'post',
                data : {
                    'dataCate' : dataCate,
                    'user'     : $("#userSpan").text()
                },
                success : function(data) {
                    $("#upload_btn").attr("disabled",false);
                    $("#uploadModal").modal("hide");
                    var outTrxObj = eval("("+data+")");
                    if(outTrxObj.rtn_code == _NORMAL){
                        layer.msg("Excel导入成功");
                        getGridData();
                    }else{
                        layer.msg(outTrxObj.rtn_mesg, function(){});
                    }
                }
            });
        }
    });

    //Excel模板导出按钮
    $("#downloadEFGR_btn").click(function (){
        generateBasicDataExcel("EFGR", "Report基础参数维护_EFGR: Hlam效率Bin值对应关系" + "_");
    });
}

/**
 * 初始化Grid表格
 */
function initGrid() {
    //表格一
    // var inTrxObj1 = {
    //     trx_id          : 'XPBASICDATA',
    //     action_flg      : 'Q',
    //     iary            : [{
    //         data_cate : 'FDBB'
    //     }]
    // };
    // var outTrxObj1 = comTrxSubSendPostJson(inTrxObj1);
    // if(outTrxObj1.rtn_code == _NORMAL) {
    //     layuiGrid1DataCache = outTrxObj1.oary;
    // }

    var options1 = _layuiGrid.f.getDefaultOptions('layuiGrid1', '#table1');
    options1.cols = [[
        {width:50, title: '复选框', type: 'checkbox'}
        ,{field:'operation',   width:140,     title: '操作',      toolbar:"#tableBar2"}
        ,{field:'dataId',      width:120,     title: '分档版本',  sort: true, edit: false}
        ,{field:'ext1',        minWidth:220,  title: '对应线别',  sort: true, edit: true}
        ,{field:'operation',   width:120,     title: '操作',      toolbar:"#tableBar1"}
    ]];
    // options1.data = layuiGrid1DataCache;
    layui.use('table', function(){
        layui.table.render(options1);

        //监听工具条
        layui.table.on('tool(table1)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ' 的查看操作');
            } else if(obj.event === 'del'){
                layer.confirm('是否删除行数据?', function(index){
                    deleteGridData([data]);
                    layer.close(index);
                    getGrid1Data();
                });
            } else if(obj.event === 'edit'){
                updateGridData([data]);
            } else if(obj.event === 'query'){
                var iary = {
                    dataCate        : 'EFGR',
                    dataExt         : data.dataId
                };
                iary2Cache = iary;
                getGrid2Data(iary);
            }
        });
    });

    //表格二
    var options2 = _layuiGrid.f.getDefaultOptions('layuiGrid2', '#table2');
    options2.cols = [[
        {width:50, title: '复选框', type: 'checkbox'}
        ,{field:'operation', width:140, title: '操作',      toolbar:"#tableBar2"}
        ,{field:'dataSeqId', width:150, title: 'dataSeqId', sort: true, hide: true}
        ,{field:'dataCate',  width:150, title: 'dataCate',  sort: true, hide: true}
        ,{field:'dataExt',   width:120, title: '分档版本',  sort: true, edit: false}
        ,{field:'dataId',    width:120, title: '单/多晶',   sort: true, edit: true}
        ,{field:'ext1',      width:120, title: '效率' ,     sort: true, edit: true}
        ,{field:'ext2',      width:120, title: 'Bin值',     sort: true, edit: true}
        ,{field:'evtUsr',       width:120, title: '修改人',   sort: true, edit: true}
        ,{field:'evtTimestamp', width:160, title: '修改时间', sort: true, edit: true}
    ]];
    layui.use('table', function(){
        layui.table.render(options2);

        //监听工具条
        layui.table.on('tool(table2)', function(obj){
            var data = obj.data;
            if(obj.event === 'detail'){
                layer.msg('ID：'+ data.id + ' 的查看操作');
            } else if(obj.event === 'del'){
                layer.confirm('是否删除行数据?', function(index){
                    deleteGridData([data]);
                    layer.close(index);
                    getGrid2Data(iary2Cache);
                });
            } else if(obj.event === 'edit'){
                updateGridData([data]);
            }
        });
    });
}

/**
 * 初始化
 */
function init() {
    layui.config({version: '1555120190807'}); //更新JS缓存
}


$(document).ready(function() {
    init();
    initGrid();
    initEvents();
});
